package tw.asap.plus.ui.custom;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import tw.asap.plus.R;
import tw.asap.plus.utils.ScreenUtil;

public class ImageTextButton extends LinearLayout {
    private TextView mTextView;
    private ImageView mImageView;

    public ImageTextButton(Context context) {
        super(context);
    }

    public ImageTextButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageText);
        String mText = a.getString(R.styleable.ImageText_text);
        int mIconId = a.getResourceId(R.styleable.ImageText_image, 0);
        boolean mSelected = a.getBoolean(R.styleable.ImageText_text_sel, false);
        float textSize = a.getDimension(R.styleable.ImageText_text_size, ScreenUtil.dip2px(context, 10));
        float gap = a.getDimension(R.styleable.ImageText_gap, ScreenUtil.dip2px(context,3));
        int color = a.getResourceId(R.styleable.ImageText_text_color, 0);
        a.recycle();
        mImageView = new ImageView(context);
        mImageView.setImageResource(mIconId);
        mImageView.setPadding(0, ScreenUtil.dip2px(context, 4), 0, ScreenUtil.dip2px(context,gap));

        mTextView = new TextView(context);
        mTextView.setText(mText);
        mTextView.setTextSize(ScreenUtil.px2dip(context,textSize));
        if(color == 0) {
            ColorStateList colorStateList = getResources().getColorStateList(R.color.text_gray_selector);
            mTextView.setTextColor(colorStateList);
        }else{
            mTextView.setTextColor(context.getResources().getColor(color));
        }
        mTextView.setGravity(Gravity.CENTER_HORIZONTAL);

        setSelected(mSelected);
        this.setOrientation(VERTICAL);
        this.addView(mImageView);
        this.addView(mTextView, 1);
    }

    public void setText(String text) {
        if (mTextView != null) {
            mTextView.setText(text);
        }
    }

    public void setImage(int resId) {
        if (mImageView != null) {
            mImageView.setImageResource(resId);
        }
    }

    public View getImageView() {
        return mImageView;
    }

    public void setSelected(boolean isSelect) {
        if (isSelect) {
            mImageView.setSelected(true);
            mTextView.setSelected(true);
        } else {
            mImageView.setSelected(false);
            mTextView.setSelected(false);
        }
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    public ImageTextButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ImageTextButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}
